home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr10 / 184_01.zip / FPC.C < prev    next >
Text File  |  1993-06-13  |  3KB  |  111 lines

  1. /* fpc.c--tests conversion to/from amd9511 fpp format 
  2.  
  3. Used to verify format conversion routines used in FLTLB.
  4. See accompanying documentation.  Other useful information
  5. is in my article "Faster floating point math," which appeared
  6. on pp.46-54 of the Nov/Dec 1985 issue of Micro/Systems Journal.
  7. */
  8.  
  9. #include fprintf.h
  10. #include c80def.h
  11.  
  12. #define MESSAGE "\nFloating-point format conversion program")
  13. #define DASHES "\n\n----------------------------------------")
  14.  
  15. #define MAX 6    /* how many different values to feed 
  16.              to the format conversion routines */
  17.  
  18. /* replace the next two functions with 
  19.      your own format conversion routines as needed */
  20. extern long c2amd();    /* link .rel file containing these */
  21. extern float amd2c();    /*   to fpc.rel */
  22.  
  23. main()
  24. {
  25.     int i;
  26.     static int sx=10;
  27.     float x;
  28.     static float dx=1.0;
  29.  
  30.     printf(MESSAGE);
  31.     printf(DASHES);
  32.  
  33.     /* a geometric series of positive values */
  34.     for (i = 0, x = 100.0; i<=MAX; x /= sx, i++) showbits(x);
  35.     printf(DASHES);
  36.  
  37.     /* a geometric series of negative values */
  38.     for (i = 0, x = -100.0; i<=MAX; x /= sx, i++) showbits(x);
  39.     printf(DASHES);
  40.  
  41.     /* a linear series of positive and negative values */
  42.     for (i = -MAX, x=(float)(-MAX); i<=MAX; x += dx, i++) showbits(x);
  43. }
  44.  
  45.  
  46. /* show bit patterns used by C80 and AMD FPP floating point formats 
  47.      to represent the float n */
  48. showbits(n)
  49. float n;
  50. {
  51.     int i;
  52.     union {
  53.         float f;
  54.         long l;
  55.     } x,z;    /* unions are easier to use here than pointers */
  56.     long y;
  57.  
  58.     x.f=n;
  59.     /* show C80's preconversion bit pattern */
  60.     printf("\n\nx = %e,\t\tBCDE = ",x.f);
  61.     prntlong(x.l);
  62.  
  63. /* eliminate next few lines if you just want to check the format
  64.      used by your version of C */
  65.     /* show AMD formatted data */
  66.     y=c2amd(x.f);
  67.     printf("\n\t\t\t\t AMD = ");
  68.     prntlong(y);
  69.  
  70.     /* convert back to C80's format */
  71.     z.f=amd2c(y);
  72.     printf("\nz = %e,\t\tBCDE = ",z.f);
  73.     prntlong(z.l);
  74. /* end of C80 -> AMD -> C80 format conversion test */
  75. }
  76.  
  77.  
  78. /* print bit pattern of a long, starting with the high-order bit
  79.      of the most significant byte, working down from left to right */
  80. prntlong(k)
  81. long k;
  82. {
  83.     int i;
  84.     union {
  85.         long l;
  86.         char b[4];
  87.     } datum;
  88.  
  89.     datum.l=k;
  90.     for (i=3; i>=0; i--) {
  91.         prntbyt(datum.b[i]);
  92.         printf(" ");
  93.     }
  94. }
  95.  
  96.  
  97. /* print the bit pattern for a byte from left to right, 
  98.      high order bit first (does the dirty work for prntlong) */
  99. prntbyte(i)
  100. int i;
  101. {
  102.     int j;
  103.     char bit;
  104.  
  105.     for (j=0x80; j>0; j=j>>1) {
  106.         if (i & j) bit='1';
  107.         else bit='0';
  108.         printf("%c",bit);
  109.     }
  110. }
  111.